import * as API from '../../../api/pedestal';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    showTradePicker: false,
    tempTrade: null,
    showPicker:"",
    id: null,
    form: {
      pedestalName: null,
      eatNumber: null,
      sort: null,
      pedestalTypeId: null,
      pedestalTypeName: null
    }
  },
  openTradePicker: function () {
    this.setData({
      showTradePicker: true,
      tempTrade: this.data.dataList[0],
      showPicker: "show"
    })
  },
  bindChangeTrade: function (e) {
    var tempTrade = this.data.dataList[e.detail.value[0]];
    console.log(e, 9999)
    this.setData({
      tempTrade: tempTrade
    })
  },
  closePicker: function () {
    this.setData({
      showTradePicker: false,
      showPicker: ""
    })
  },
  cancelChange: function (e) {
    this.setData({
      showTradePicker: false,
      tempTrade: null,
      showPicker: ""
    })
  },
  ensureChange: function (e) {
    let pedestalTypeId = 'form.pedestalTypeId'
    let pedestalTypeName = 'form.pedestalTypeName'
    this.setData({
      showTradePicker: false,
      [pedestalTypeId]: this.data.tempTrade.id,
      [pedestalTypeName]: this.data.tempTrade.pedestalTypeName
    })
  },
  // 提交表单
  formSubmit: function (e) {
    if (this.validForm(e)) {
      let postbody = e.detail.value
      postbody.pedestalTypeId = this.data.form.pedestalTypeId
      // postbody.commodityImg = this.data.form.commodityImg
      // postbody.commoditySpecificationsDtos = this.data.form.commoditySpecificationsDtos
      // postbody.commodityChannelId = '1345252220928643073'
      if(this.data.id){
        API.editCommodity(this.data.id, postbody).then(res => {
          wx.navigateBack({});
          wx.showToast({
            icon: 'success',
            title: '修改成功',
            duration: 1500
          });
        }, err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: err || '修改失败'
          });
        })
      }else{
        API.addCommodity(postbody).then(res => {
          wx.navigateBack({});
          wx.showToast({
            icon: 'success',
            title: '添加成功',
            duration: 1500
          });
        }, err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: err || '添加失败'
          });
        })
      }
    }
  },
  validForm(e) {
    var pedestalName = e.detail.value.pedestalName;
    var pedestalTypeId =  this.data.form.pedestalTypeId;
  
    if (!pedestalName || pedestalName < 1) {
      wx.showToast({
        title: '餐桌名称不能为空',
        icon: 'none'
      });
      return false;
    }
  
    if (!pedestalTypeId) {
      wx.showToast({
        title: '餐桌分类不能为空',
        icon: 'none'
      });
      return false;
    }
    return true;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, 222222222)
    this.setData({
      id: options.id||null
    })
    this.data.id&&this.getDetail()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.getList()
  },
  getList(){
    wx.showLoading ({});
    API.getProductType().then(res => {
      wx.hideLoading()
      this.setData({
        dataList: res.data||[]
      })
    }, err => {
      wx.hideLoading();
    })
  },
  getDetail(){
    wx.showLoading ({});
    API.getCommodityDetail(this.data.id).then(res => {
      wx.hideLoading()
      this.setData({
        form: res.data||[]
      })
    }, err => {
      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})